import pyautogui
import time
import win32gui
import win32con
import pydirectinput


def Choose(wt):
    # 鸣徽选择
    cnt = 0
    while True:
        try:
            pyautogui.locateOnScreen('./img/part4.png', region=(510, 780, 550, 820), grayscale=True, confidence=0.9)
            time.sleep(0.5 + wt)
            pyautogui.click(530, 800)
            break
        except pyautogui.ImageNotFoundException:
            time.sleep(1)
            cnt += 1
            if cnt >= 10:
                return False
    return True

def Quit():
    # 退出
    pydirectinput.press('esc')
    time.sleep(1)
    pyautogui.click(1721, 1031)
    time.sleep(1)
    pyautogui.click(1125, 625)
    time.sleep(1)
    cnt = 0
    while True:
        try:
            pyautogui.locateOnScreen('./img/part12.png', region=(1670, 1015, 1700, 1040), grayscale=True,
                                     confidence=0.9)
            time.sleep(2)
            pyautogui.click(1700, 1025)
            break
        except pyautogui.ImageNotFoundException:
            time.sleep(1)
            cnt += 1
            if cnt >= 10:
                return False
    return True


def Boom():
    # 炸弹
    cnt = 0
    pyautogui.click(115, 760)
    time.sleep(1)
    while True:
        try:
            pyautogui.locateOnScreen('./img/part9.png', region=(950, 770, 980, 810), grayscale=True, confidence=0.9)
            pyautogui.click(950, 800, duration=0.2)
            break
        except pyautogui.ImageNotFoundException:
            pyautogui.click(115, 760)
            time.sleep(1)
            cnt += 1
            if cnt >= 10:
                return False
    return True


def ScriptStart1(flag1, flag2, wt):
    # 服务器选择
    hwnd = win32gui.FindWindow(None, '绝区零' if not flag1 else 'ZenlessZoneZero')
    if not win32gui.IsWindow(hwnd):
        return 0
    else:
        win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
        win32gui.SetForegroundWindow(hwnd)
    while True:
        # 副本选择界面
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part1.png', region=(1370, 1000, 1420, 1050), grayscale=True, confidence=0.9)
                time.sleep(0.5 + wt)
                pyautogui.click(667, 686)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1)
        cnt = 0
        # 难度选择界面
        while True:
            try:
                pyautogui.locateOnScreen('./img/part2.png', region=(1300, 1000, 1511, 1100), grayscale=True, confidence=0.9)
                time.sleep(0.2 + wt)
                pyautogui.click(1396, 402)
                time.sleep(0.2 + wt)
                pyautogui.click(1700, 1030)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1)
        cnt = 0
        # 队员选择界面
        while True:
            try:
                pyautogui.locateOnScreen('./img/part3.png', region=(434, 140, 486, 180), grayscale=True, confidence=0.9)
                time.sleep(0.5 + wt)
                pyautogui.click(1726, 1028)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(15)
        # 鸣徽选择界面
        if not flag2:
            if not Choose(wt):
                return 1
        # 研究者对话界面
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part17.png', region=(1295, 400, 1330, 434), grayscale=True, confidence=0.9)
                time.sleep(2 + wt)
                pyautogui.click(1800, 468, clicks=2)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(2 + wt)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1 + wt)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1)
        # 研究者选择界面
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part5.png', region=(1390, 660, 1425, 700), grayscale=True, confidence=0.9)
                time.sleep(0.2 + wt)
                pyautogui.click(1435, 685)
                break
            except pyautogui.ImageNotFoundException:
                pyautogui.click(1800, 468, clicks=2)
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1 + wt)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1 + wt)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(5)
        flag = 1
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part6.png', region=(1290, 210, 1330, 240), grayscale=True, confidence=0.9)
                break
            except pyautogui.ImageNotFoundException:
                try:
                    pyautogui.locateOnScreen('./img/part7.png', region=(1290, 876, 1330, 900), grayscale=True, confidence=0.9)
                    flag = 0
                    break
                except pyautogui.ImageNotFoundException:
                    time.sleep(1)
                    cnt += 1
                    if cnt >= 10:
                        return 1
        cnt = 0
        time.sleep(1 + wt)
        if flag:
            if not Boom():
                return 1
            time.sleep(3)
            # 向右边走一格触发鸣徽选择
            pyautogui.click(1137, 501)
            time.sleep(1)
            if not Choose(wt):
                return 1
            time.sleep(1 + wt)
            pyautogui.click(1350, 160)
        else:
            time.sleep(wt)
            pyautogui.click(955, 636)
            time.sleep(2 + wt)
            pyautogui.click(1800, 468, clicks=2)
            time.sleep(1)
            while True:
                try:
                    pyautogui.locateOnScreen('./img/part8.png', region=(1330, 650, 1380, 700), grayscale=True,
                                             confidence=0.9)
                    time.sleep(1 + wt)
                    pyautogui.click(1488, 686, clicks=2)
                    break
                except pyautogui.ImageNotFoundException:
                    time.sleep(1)
                    cnt += 1
                    if cnt >= 10:
                        return 1
            time.sleep(1 + wt)
            pyautogui.click(1800, 468, clicks=2)
            time.sleep(1)
            # 炸掉
            if not Boom():
                return 1
            time.sleep(3)
            # 去下面那格
            time.sleep(wt)
            pyautogui.click(955, 840)
            # 触发鸣徽选择界面
            if not Choose(wt):
                return 1
            time.sleep(1 + wt)
            pyautogui.click(1350, 850)
        # 进入boss选择界面
        time.sleep(2)
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part8.png', region=(1330, 650, 1380, 700), grayscale=True, confidence=0.9)
                time.sleep(1 + wt)
                pyautogui.click(1488, 581)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(15)
        # 进入战斗
        while True:
            try:
                center = pyautogui.locateCenterOnScreen('./img/part10.png', region=(1120, 700, 1167, 1080), grayscale=True, confidence=0.9)
                time.sleep(1 + wt)
                pyautogui.click(center.x, center.y)
                break
            except pyautogui.ImageNotFoundException:
                for i in range(5):
                    pydirectinput.keyDown('w')
                    pydirectinput.press('k', presses=2, interval=0.2)
                    pydirectinput.press('j', presses=6, interval=0.2)
                    pydirectinput.keyUp('w')
                    time.sleep(0.5)
        time.sleep(1)
        if not Choose(wt):
            return 1
        time.sleep(7)
        pyautogui.click(950, 320)
        time.sleep(1)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1)
        pyautogui.click(1800, 468, clicks=2)
        cnt = 0
        # 研究者物资选择界面
        while True:
            try:
                center = pyautogui.locateCenterOnScreen('./img/part11.png', region=(1326, 480, 1426, 770), grayscale=True, confidence=0.9)
                time.sleep(0.5 + wt)
                pyautogui.click(center.x, center.y)
                break
            except pyautogui.ImageNotFoundException:
                try:
                    center = pyautogui.locateCenterOnScreen('./img/part13.png', region=(1326, 480, 1426, 770), grayscale=True,
                                                            confidence=0.9)
                    time.sleep(0.5 + wt)
                    pyautogui.click(center.x, center.y)
                    break
                except pyautogui.ImageNotFoundException:
                    try:
                        center = pyautogui.locateCenterOnScreen('./img/part15.png', region=(1326, 480, 1426, 770), grayscale=True,
                                                                confidence=0.9)
                        time.sleep(0.5 + wt)
                        pyautogui.click(center.x, center.y)
                        time.sleep(1 + wt)
                        pyautogui.click(975, 734)
                        break
                    except pyautogui.ImageNotFoundException:
                        try:
                            center = pyautogui.locateCenterOnScreen('./img/part16.png', region=(1326, 480, 1426, 770),grayscale=True,
                                                                    confidence=0.9)
                            time.sleep(0.5 + wt)
                            pyautogui.click(center.x, center.y)
                            time.sleep(1 + wt)
                            pyautogui.click(975, 800)
                            time.sleep(1 + wt)
                            cnt = 0
                            while True:
                                try:
                                    center = pyautogui.locateCenterOnScreen('./img/part24.png', region=(946, 631, 979, 668),
                                                                            grayscale=True,
                                                                            confidence=0.9)
                                    time.sleep(1 + wt)
                                    pyautogui.click(center.x, center.y)
                                    break
                                except pyautogui.ImageNotFoundException:
                                    time.sleep(1)
                                    cnt += 1
                                    if cnt >= 5:
                                        break
                            break
                        except pyautogui.ImageNotFoundException:
                            pyautogui.click(1800, 468)
                            time.sleep(1)
                            cnt += 1
                            if cnt >= 10:
                                return 1
        time.sleep(2)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(2 + wt)
        pyautogui.click(1580, 130)
        # 如果有商店可能会绕路
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part18.png', region=(1300, 400, 1332, 437), grayscale=True, confidence=0.9)
                time.sleep(1 + wt)
                pyautogui.click(1800, 468, clicks=2)
                break
            except pyautogui.ImageNotFoundException:
                pyautogui.click(1580, 130)
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(1)
        pyautogui.click(1800, 468, clicks=2)
        time.sleep(3)
        pyautogui.click(1150, 315, clicks=2)
        time.sleep(1)
        cnt = 0
        # 业绩确认
        while True:
            try:
                pyautogui.locateOnScreen('./img/part14.png', region=(940, 705, 980, 750), grayscale=True, confidence=0.9)
                time.sleep(1 + wt)
                pyautogui.click(975, 734)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1)
        Quit()
        # 进入下一轮循环
        time.sleep(5)


def ScriptStart2(flag, wt):
    hwnd = win32gui.FindWindow(None, '绝区零' if not flag else 'ZenlessZoneZero')
    if not win32gui.IsWindow(hwnd):
        return 0
    else:
        win32gui.ShowWindow(hwnd, win32con.SW_SHOWNORMAL)
        win32gui.SetForegroundWindow(hwnd)
    while True:
        cnt = 0
        remake = 0
        sleep = False
        while True:
            # 判断是否睡觉
            try:
                pyautogui.locateOnScreen('./img/part19.png', region=(1520, 39, 1546, 70), grayscale=True, confidence=0.9)
                time.sleep(1 + wt)
                pyautogui.click(887, 740)
                break
            except pyautogui.ImageNotFoundException:
                try:
                    pyautogui.locateOnScreen('./img/part20.png', region=(600, 900, 1330, 960), grayscale=True, confidence=0.8)
                    time.sleep(1 + wt)
                    pyautogui.click(1309, 924, clicks=2)
                    time.sleep(2 + wt)
                    pyautogui.click(1309, 924, clicks=2)
                    # 去睡觉
                    time.sleep(3)
                    pydirectinput.keyDown('d')
                    # 玲向右边移动的时间 如果是哲 需要调整
                    time.sleep(0.15)
                    pydirectinput.keyUp('d')
                    pydirectinput.keyDown('s')
                    time.sleep(1.5)
                    pydirectinput.keyUp('s')
                    pydirectinput.keyDown('d')
                    time.sleep(0.5)
                    pydirectinput.keyUp('d')
                    pydirectinput.press('f')
                    sleep = True
                    break
                except pyautogui.ImageNotFoundException:
                    pydirectinput.press('f')
                    time.sleep(1)
                    cnt += 1
                    if cnt >= 10:
                        return 1
        if sleep:
            continue
        cnt = 0
        time.sleep(0.5)
        pyautogui.moveTo(1374, 477)
        while True:
            try:
                center = pyautogui.locateCenterOnScreen('./img/part21.png', region=(1090, 205, 1136, 871), grayscale=True, confidence=0.9)
                time.sleep(0.2 + wt)
                pyautogui.click(center.x, center.y)
                time.sleep(0.2)
                pyautogui.click(1649, 1026)
                break
            except pyautogui.ImageNotFoundException:
                pyautogui.vscroll(-100)
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(1 + wt)
        pyautogui.click(1730, 1030)
        time.sleep(12)
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part22.png', region=(146, 56, 185, 84), grayscale=True, confidence=0.9)
                time.sleep(0.2)
                break
            except pyautogui.ImageNotFoundException:
                time.sleep(0.2)
                cnt += 1
                if cnt >= 50:
                    return 1
        # 进入战斗
        pydirectinput.keyDown('d')
        pydirectinput.keyDown('w')
        pydirectinput.press('k')
        pydirectinput.keyUp('d')
        pydirectinput.press('j')
        pydirectinput.press('space')
        for i in range(3):
            pydirectinput.press('k', presses=2)
        pydirectinput.press('d', presses=3)
        pydirectinput.keyUp('w')
        pydirectinput.press('f')
        cnt = 0
        while True:
            try:
                center = pyautogui.locateCenterOnScreen('./img/part23.png',
                                                        region=(1479, 653, 1505, 681),
                                                        grayscale=True, confidence=0.9)
                time.sleep(0.2 + wt)
                pyautogui.click(center.x, center.y)
                break
            except pyautogui.ImageNotFoundException:
                pyautogui.click(1470, 993, clicks=2)
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    pydirectinput.press('esc')
                    cnt = 0
                    while True:
                        try:
                            center = pyautogui.locateCenterOnScreen('./img/part25.png',
                                                                    region=(1668, 1013, 1701, 1048),
                                                                    grayscale=True, confidence=0.9)
                            time.sleep(0.2 + wt)
                            pyautogui.click(center.x, center.y)
                            time.sleep(0.2 + wt)
                            pyautogui.click(1112, 629)
                            remake = 1
                            break
                        except pyautogui.ImageNotFoundException:
                            pyautogui.click(1470, 993, clicks=2)
                            time.sleep(1)
                            cnt += 1
                            if cnt >= 10:
                                return 1
                    break
        if remake:
            time.sleep(3)
            continue
        time.sleep(1 + wt)
        pyautogui.click(1470, 993, clicks=2)
        time.sleep(2)
        cnt = 0
        while True:
            try:
                pyautogui.locateOnScreen('./img/part12.png', region=(1670, 1015, 1700, 1040), grayscale=True, confidence=0.9)
                time.sleep(2 + wt)
                pyautogui.click(1700, 1025)
                break
            except pyautogui.ImageNotFoundException:
                pyautogui.click(1470, 993, clicks=2)
                time.sleep(1)
                cnt += 1
                if cnt >= 10:
                    return 1
        time.sleep(3)